package com.aiti.lulian.controller.crm;

import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.crm.CrmListFieldConfigDto;
import com.aiti.lulian.entity.SysData;
import com.aiti.lulian.service.ICrmListFieldConfigService;
import com.aiti.lulian.vo.crm.CrmListFieldModuleVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "CRM列表字段配置控制层")
@RequestMapping("/crm/list/field")
public class CrmListFieldConfigController extends WebController {

    @Autowired
    private ICrmListFieldConfigService crmListFieldConfigService;

    @ApiOperation("新增或更新指定模块列表字段的配置")
    @PostMapping("/config/addOrUpdate")
    public Message addOrUpdateListFieldConfig(@RequestBody CrmListFieldConfigDto dto) {
        String userId = dto.getUserId();
        if (StrUtil.isBlank(userId)) {
            userId = findUserId();
            if (StrUtil.isBlank(userId)) {
                return Fail("请先登录");
            }
            dto.setUserId(userId);
        }
        return crmListFieldConfigService.addOrUpdateListFieldConfig(dto);
    }

    @ApiOperation("查询当前用户指定模块列表字段的配置")
    @GetMapping("/config")
    public Message getListFieldConfig(@RequestParam(value = "userId", required = false) String userId,
                                      @RequestParam(value = "enterpriseId") String enterpriseId,
                                      @RequestParam(value = "module") String module) {
        if (StrUtil.isBlank(userId)) {
            userId = findUserId();
            if (StrUtil.isBlank(userId)) {
                return Fail("请先登录");
            }
        }
        return Success(crmListFieldConfigService.getListFieldConfig(userId, enterpriseId, module));
    }

    @ApiOperation("查询企业自定义列的可配置列表")
    @GetMapping("/configurable")
    public Message getListConfigurableField(@RequestParam(value = "userId", required = false) String userId,
                                            @RequestParam("enterpriseId") String enterpriseId,
                                            @RequestParam("pageNum") Integer pageNum,
                                            @RequestParam("pageSize") Integer pageSize) {
        if (StrUtil.isBlank(userId)) {
            userId = findUserId();
            if (StrUtil.isBlank(userId)) {
                return Fail("请先登录");
            }
        }
        Page<SysData> page = startPage(pageNum, pageSize);
        List<CrmListFieldModuleVo> resultList = crmListFieldConfigService.getListConfigurableField(userId, enterpriseId, page);
        return Success(getPageResult(resultList, pageNum, pageSize, page.getTotal()));
    }
}
